package com.cmwa.solution.sys.msg.adapter.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.cmwa.solution.sys.msg.adapter.EmailMessageTemplate;
import com.cmwa.solution.sys.msg.constant.MessageOperationConstant;
import com.cmwa.solution.sys.msg.vo.ManualOperationParamsVo;
import com.cmwa.solution.sys.msg.vo.MsgConfVo;
import com.cmwa.solution.sys.msg.vo.MsgWaitSendVo;
import com.cmwa.solution.sys.msg.vo.OperationResultVo;

/**
 * 
 * @author 	huyong
 * @date 	2015年10月19日
 * @time 	下午8:19:04
 */
public abstract class AutoOperationMessageImpl extends AbstractEmailMessageServerce implements EmailMessageTemplate {

	private EmailMessageTemplateDelegate emailMessageTemplateDelegate;

	public AutoOperationMessageImpl() {
		this.emailMessageTemplateDelegate = new EmailMessageTemplateDelegate(MessageOperationConstant.OPERATION_TYPE_AUTO,this);
	}

	@Override
	public final OperationResultVo execute(ManualOperationParamsVo params) {
		OperationResultVo rst = new OperationResultVo();

		if (params != null) {
			MsgConfVo conf = emailMessageTemplateDelegate.obtainMsgConf(params);
			if (conf == null) {
				rst.setResultCode("M002");
				rst.setResultContent("请确认对应消息配置是否存在并处于运行状态");
				return rst;
			}
			Map<String, List<String>> confEmps = emailMessageTemplateDelegate.obtainMsgEmpsConf(params);
			if (confEmps == null || confEmps.size() == 0) {
				rst.setResultCode("M003");
				rst.setResultContent("请确认消息配置对应是否配有人员");
				return rst;
			}

			Map<String, List<Map<String, String>>> empsEmailAddress = emailMessageTemplateDelegate.obtainEmailAddressByEmpIds(confEmps);
			if ((empsEmailAddress == null || empsEmailAddress.size() == 0) && !params.isThirdReceiver()) {
				rst.setResultCode("M004");
				rst.setResultContent("请确认消息配置对应人员邮箱配置是否正常");
				return rst;
			}

			List<Map<String, Object>> datas = obtainData(params);
			if (datas == null || datas.size() == 0) {
				rst.setResultCode("M005");
				rst.setResultContent("获取原始数据为空,请确认传入参数是否正确,或数据是否符合筛选条件");
				return rst;
			}

			List<MsgWaitSendVo> msgs = assembleMessage(conf, confEmps, empsEmailAddress, datas, params);
			if (msgs == null || msgs.size() == 0) {
				rst.setResultCode("M006");
				rst.setResultContent("最终组装结果为空,没有需要发送的消息");
				return rst;
			}
			
			// 2017/12/20 根据id获取邮箱，整合到最终收件人、抄送人集合
			boolean emailReuslt = emailMessageTemplateDelegate.obtainEmailAddress(msgs);
			if (!emailReuslt) {
				rst.setResultCode("M007");
				rst.setResultContent("最终组装收件人邮箱地址为空");
				return rst;
			}

			String st = StringUtils.trimToEmpty(params.getSendTime());
			if (ManualOperationParamsVo.SENDTIME_LATER.equalsIgnoreCase(st)) {
				emailMessageTemplateDelegate.saveNewMessage(msgs, params);
			}
			if (ManualOperationParamsVo.SENDTIME_NOW.equalsIgnoreCase(st)) {
				if (ManualOperationParamsVo.SENDTIME_NOW_ASYN.equalsIgnoreCase(params.getSendTimeAsyn())) {
					emailMessageTemplateDelegate.sendMessageAsyn(msgs, params);
				} else {
					try {
						emailMessageTemplateDelegate.sendMessage(msgs, params);
					} catch (Exception e) {
						e.printStackTrace();
						rst.setResultCode("ERROR");
						rst.setResultContent(e.getMessage());
						return rst;
					}
				}
			}
			rst.setResultCode("SUCCESS");
			rst.setResultContent("邮件发送成功");
			return rst;
		} else {
			rst.setResultCode("M001");
			rst.setResultContent("没有指定需要发送的内容");
			return rst;
		}
	}
	
}
